vertShape = 
"""
precision highp float;
attribute vec2 vertex;
attribute vec4 vertexColor;

uniform vec4 screen;

varying lowp vec4 color;
void main() { 
  gl_Position = vec4(vertex, 1.0, 1.0) * screen; 
  color = vertexColor;
}
"""

fragShape = 
"""
precision highp float;
varying lowp vec4 color;

void main() { 
  gl_FragColor = color; 
}
"""

class window.ShapeRender
  constructor : (@g) ->
    @buffOutline = @g.ctx.MeshBuffer()
    @buffOutline.addDescription "vertex", "FLOAT", 2
    @buffOutline.addDescription "vertexColor", "FLOAT", 4


    @buffSolid = @g.ctx.MeshBuffer()
    @buffSolid.addDescription "vertex", "FLOAT", 2
    @buffSolid.addDescription "vertexColor", "FLOAT", 4

    @prog = @g.ctx.Program()
    @prog.setFragSource fragShape
    @prog.setVertSource vertShape
    @prog.link()
    @io = 0
    @is = 0
  
  setTransform : (world) ->
    @prog.um3f world.getMatrix()


  drawLine : (x0, y0, x1, y1, r, g, b, a) ->
    @buffOutline.pushff x0, y0
    @buffOutline.pushffff r, g, b, a

    @buffOutline.pushff x1, y1
    @buffOutline.pushffff r, g, b, a

    @buffOutline.pushii @io, @io + 1
    @io += 2

    
  drawRectFilled : (x0, y0, x1, y1, r, g, b, a) ->
    @buffSolid.pushff x0, y0
    @buffSolid.pushffff r, g, b, a
    @buffSolid.pushff x1, y0
    @buffSolid.pushffff r, g, b, a
    @buffSolid.pushff x1, y1
    @buffSolid.pushffff r, g, b, a
    @buffSolid.pushff x0, y1
    @buffSolid.pushffff r, g, b, a

    @buffSolid.pushiii @is, @is + 1, @is + 2
    @buffSolid.pushiii @is, @is + 3, @is + 2

    @is += 4
    

  render : () ->
    @g.ctx.gl.lineWidth 4.0
    
    @buffOutline.upload()
    @buffSolid.upload()
    @prog.use()

    @buffOutline.setup @prog
    @buffOutline.draw(@g.ctx.gl.LINES)
    @buffOutline.clear()


    @buffSolid.setup @prog
    @buffSolid.draw()
    @buffSolid.clear()

    @io = 0
    @is = 0
    
